/**
 * 
 */
package business;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;

import model.Adresse;
import model.Datum;
import model.Kunde;
import model.Name;
import model.Person;
import db.DbManager;
import db.PersonSource;

/**
 * @author andris
 *
 */
class KundenContainer implements KundenLogik {
	
	private static KundenContainer instance = null;
	
	public static KundenContainer getInstance(){
		if(instance == null){
			instance = new KundenContainer();
		}
		return instance;
	}
	
	
	private List<Kunde> kundenListe = null;
	private PersonSource kundenSource;
	private boolean isClosed = false;
	
	/**
	 * 
	 */
	private KundenContainer(){
		initialize();
	}
	
	/**
	 * 
	 */
	private void initialize() {
		if(isClosed)
			throw new IllegalStateException("method invoked on closed instance");
		
		kundenSource = DbManager.getInstance();
		
		List<Person> personen = kundenSource.getAllPerson();
		kundenListe = new ArrayList<Kunde>();
		
		for(Person person : personen){
			if(person instanceof Kunde){
				kundenListe.add( (Kunde)person );
			}
		}
		
		System.out.println(kundenListe);
		
	}

	/* (non-Javadoc)
	 * @see business.KundenHandler#getKunde(int)
	 */
	@Override
	public Kunde getKunde(int kundennummer) {
		if(isClosed)
			throw new IllegalStateException("method invoked on closed instance");
		
		int index = indexOf(kundennummer);
		if( 0 <= index){
			return kundenListe.get(index);
		}
		
		return null;
	}

	/* (non-Javadoc)
	 * @see business.KundenHandler#getKundenListeZuNummer(int)
	 */
	@Override
	public List<Kunde> getKundenListeZuNummer(String kundennummer) {
		if(isClosed)
			throw new IllegalStateException("method invoked on closed instance");
		
		List<Kunde> list = new ArrayList<Kunde>();
		
		for(Kunde kunde : kundenListe){
			if( String.valueOf(kunde.getKundennr()).contains(kundennummer) ){
				list.add(kunde);
			}
		}
		
		return list;
	}

	/* (non-Javadoc)
	 * @see business.KundenHandler#getKundenListeZuName(java.lang.String)
	 */
	@Override
	public List<Kunde> getKundenListeZuName(String name) {
		if(isClosed)
			throw new IllegalStateException("method invoked on closed instance");

		List<Kunde> list = new ArrayList<Kunde>();
		
		for(Kunde kunde : kundenListe){
			if( kunde.getName().toString().contains(name) ){
				list.add(kunde);
			}
		}
		
		return list;
	}

	/* (non-Javadoc)
	 * @see business.KundenHandler#updateKunde(model.Kunde)
	 */
	@Override
	public void updateKunde(Kunde kunde) throws Exception {
		if(isClosed)
			throw new IllegalStateException("method invoked on closed instance");
		
		int index = indexOf(kunde.getKundennr());
		
		if( 0 > index){
			throw new Exception("nix da fuer ein update");
		}
		
		//TODO:
		//datasource.update(kunde);
		
		kundenListe.remove(index);
		kundenListe.add(kunde);
	}

	/* (non-Javadoc)
	 * @see business.KundenHandler#saveKunde(model.Kunde)
	 */
	@Override
	public void saveKunde(Kunde kunde) {
		if(isClosed)
			throw new IllegalStateException("method invoked on closed instance");
		
		if( 0 <= indexOf(kunde.getKundennr())){
			try {
				updateKunde(kunde);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		//TODO:
		//datasource.add(kunde);
		kundenListe.add(kunde);
	}

	/* (non-Javadoc)
	 * @see business.KundenHandler#removeKunde(int)
	 */
	@Override
	public Kunde removeKunde(int kundennummer) {
		if(isClosed)
			throw new IllegalStateException("method invoked on closed instance");
		
		int index = indexOf(kundennummer);
		
		if (index < 0){
			return null;
		}
		
		return kundenListe.remove(index);
	}

	
	/* (non-Javadoc)
	 * @see business.KundenHandler#close()
	 */
	public void close(){
		//datasource.close();
		
		//datasource = null;
		this.kundenListe = null;
		KundenContainer.instance = null;
	}
	
	private int indexOf(int kundennummer){
		
		for(int i=0;i<kundenListe.size();i++){
			if(kundenListe.get(i).getKundennr() == kundennummer){
				return i;
			}
		}
		
		return -1;
	}
}
